<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="stylesheet" href="/style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h5>你的账户余额是：<span id="amount">&&&amount&&&</span></h5>
<button id="button">打钱</button>
<script>
    // window.xxx=function(result){
    //     alert('这是frank写的前端代码');
    //     alert(`我得到的结果是${result}`);
    //
    //     if(result==='success'){
    //         amount.innerText=amount.innerText-1;
    //     }else{
    //
    //     }
    // }
    button.addEventListener('click',(e)=>{
        //使用jquery里的jsonp
        $.ajax({
            url: "/pay",

            // The name of the callback parameter, as specified by the YQL service
            jsonp: "callback",

            // Tell jQuery we're expecting JSONP
            dataType: "jsonp",

            // Tell YQL what we want and that we want JSON
            // data: {
            //     q: "select title,abstract,url from search.news where query=\"cat\"",
            //     format: "json"
            // },

            // Work with the response
            success: function( response ) {
                console.log( response ); // server response
                if(response==='success'){
                    alert('success');
                            amount.innerText=amount.innerText-1;
                        }else{

                        }
            }
        });













        // let script=document.createElement('script');
        // let functionName='zhx'+parseInt(Math.random()*10000,10);
        // window[functionName]=function(result){
        //     alert('这是frank写的前端代码');
        //     alert(`我得到的结果是${result}`);
        //
        //     if(result==='success'){
        //         amount.innerText=amount.innerText-1;
        //     }else{
        //
        //     }
        //
        // }
        // // script.src='http:jack.com:8002/pay';
        // script.src=`/pay?callback=${functionName}`;
        // //传入xxx函数名
        // // script.src='/pay';
        //
        // document.body.appendChild(script);
        // script.onload=function (e) {
        //     alert('打钱成功');// 先返回响应结果，再返回加载结果
        //     // window.location.reload();  //刷新当前页面
        //     // amount.innerText=amount.innerText-1;
        //     // debugger;
        //
        //
        //     e.currentTarget.remove();  //生成的script标签立即移除
        //     delete window[functionName]; //删除生成的函数名functionName
        // }
        // script.onerror=function(){
        //     alert('打钱失败')
        // }
    })
</script>
</body>
</html>